﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace ChronoNES
{
    class Loader_NES : Loader
    {
        byte[] header;
        byte[] trainer;

        public Loader_NES(FileStream _fs) : base(_fs)
        {
            header = new byte[16];
            trainer = new byte[512];

            _fs.Seek(0, SeekOrigin.Begin);
            _fs.Read(header, 0, 16);

            if ((byte)((header[0x6] >> 2) & 0x1) == 0x1)
            {
                _fs.Read(trainer, 0, 512);
            }
        }

        public override Mapper GetMapper()
        {
            mapper = Mapper.Create((byte)(header[0x7] >> 4));
            mapper.Init(header[0x4], header[0x5]);

            fs.Read(mapper.pROM, 0, header[0x4] * 16 * 1024);
            if (header[0x5] > 0)
            {
                fs.Read(mapper.cROM, 0, header[0x5] * 8 * 1024);
            }

            return mapper;
        }
    }
}
